<template>
  <!-- 面试技巧 -->
  <div class="container">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-row>
        <el-col :span="6" align="center">
          <el-form-item label="关键字">
            <el-input
              v-model="formInline.user"
              size="small"
              placeholder="根据文章标题搜索"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态">
            <el-select
              v-model="formInline.region"
              size="small"
              placeholder="请选择"
            >
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" align="left" :pull="1">
          <el-form-item>
            <el-button size="small" @click="onDelList">清除</el-button>
            <el-button type="primary" size="small" @click="queryList"
              >查询</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :span="6" :push="4">
          <el-form-item>
            <el-button
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="onSubmit('add')"
              >新增技巧</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="record">
      <i class="el-icon-info"></i>
      <span>数据一共 {{ counts }} 条</span>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="序号" width="180" align="center">
      </el-table-column>
      <el-table-column prop="title" label="文章标题" width="180" align="center">
        <template #default="{ row }">
          <span>{{ row.title }}</span>
          <el-button
            type="text"
            class="el-icon-film"
            @click="video"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column prop="visits" label="阅读数" align="center">
      </el-table-column>
      <el-table-column prop="username" label="录入人" align="center">
      </el-table-column>
      <el-table-column prop="createTime" label="录入时间" align="center">
      </el-table-column>
      <el-table-column prop="state" label="状态" align="center">
        <template #default="{ row }">
          <span>{{ row.state ? '已启用' : '未启用' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="operate(scope, 'pre')">预览</el-button>
          <el-button type="text" @click="operate(scope, 'use')">{{
            scope.row.state ? '禁用' : '启用'
          }}</el-button>
          <el-button
            type="text"
            :disabled="scope.row.state ? false : true"
            @click="operate(scope, 'change')"
            >修改</el-button
          >
          <el-button
            type="text"
            :disabled="scope.row.state ? false : true"
            @click="operate(scope, 'del')"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-col :push="13">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="formList.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="formList.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
        >
        </el-pagination>
      </el-col>
    </el-row>

    <addArticles ref="addArticles" :text="text"></addArticles>
    <addPreview ref="addPreview"></addPreview>
  </div>
</template>

<script>
import { list, changeState, detail, remove } from '@/api/hmmm/articles.js'

import addArticles from '../components/articles-add.vue'
import addPreview from '../components/articles-preview.vue'

// import { formatTime } from '@/utils/index.js'

export default {
  components: {
    addArticles,
    addPreview
  },
  data () {
    return {
      counts: 1,
      formInline: {
        user: '',
        region: ''
      },
      formList: {
        page: 1,
        pagesize: 10
      },
      tableData: [],
      text: ''
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 清除
    onDelList () {
      this.formInline = {
        user: '',
        region: ''
      }
      this.getList()
    },
    onSubmit (type) {
      this.text = type
      this.$refs.addArticles.dialogVisible = true
    },
    async getList () {
      const { data } = await list(this.formList)
      // console.log(data.items)
      this.tableData = data.items
      this.counts = data.counts
    },
    handleSizeChange (val) {
      this.formList.page = 1
      this.formList.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.formList.page = val
      this.getList()
    },
    // TODO 查看视频的
    video () {
      console.log(1)
    },
    // TODO 点击修改
    async operate (rows, type) {
      // console.log(rows.row)
      if (type === 'pre') {
        const { data } = await detail(rows.row)
        console.log(data)
        this.$refs.addPreview.form = data
        this.$refs.addPreview.dialogVisible = true
      } else if (type === 'use') {
        rows.row.state = !rows.row.state
        this.$message.success('操作成功')
        await changeState(rows.row)
      } else if (type === 'change') {
        // console.log('修改')
        this.text = type
        const { data } = await detail(rows.row)
        this.$refs.addArticles.dialogVisible = true
        this.$refs.addArticles.form = data
      } else if (type === 'del') {
        // console.log('删除')
        await remove(rows.row)
        this.getList()
      }
    },
    // 查询
    async queryList () {
      // console.log(this.formInline.region)
      if (this.formInline.user || this.formInline.region) {
        this.formInline.keyword = this.formInline.user
        this.formInline.state = this.formInline.region
        // console.log(this.formInline)
        const { data } = await list(this.formInline)
        // console.log(data)
        this.tableData = data.items
        this.counts = data.counts
      }
    }
  }
}
</script>

<style scoped lang="less">
.record {
  width: 1280px;
  height: 40px;
  border-radius: 4px;
  line-height: 40px;
  margin: 0 auto;
  background-color: #f4f4f5;
  font-size: 14px;
  color: #ccc;
  span {
    margin-left: 10px;
  }
  .el-icon-info {
    font-size: 15px;
    color: #909399;
    margin-left: 15px;
  }
}
.el-col {
  margin-top: 20px;
}
.container {
  width: 1320px;
  margin: 10px auto;
  background-color: #fff;
  border-radius: 5px;
}
</style>
